package com.example.translateappjava.Utils;

import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Represents image data with recognition results and food safety information.
 * Implements Parcelable for efficient data transfer between Android components.
 */
public class ImageData implements Parcelable {
    // The actual bitmap image
    private Bitmap image = null;
    // Text result from image recognition
    private String recognitionResult = "EMPTY";
    // URL of the image source (if applicable)
    private String ImageURL = "NULL";
    // Scientific suggestion about the recognized item
    private String suggestion = "EMPTY";
    // Flag indicating whether the recognized item is edible
    private boolean eatable = true;

    /**
     * Constructor used for parcel reconstruction
     * @param in Parcel containing the serialized ImageData
     */
    protected ImageData(Parcel in) {
        image = in.readParcelable(Bitmap.class.getClassLoader());
        recognitionResult = in.readString();
        suggestion = in.readString();
        eatable = in.readByte() != 0; // true if byte != 0
    }

    /**
     * Flatten this object into a Parcel
     * @param dest The Parcel in which the object should be written
     * @param flags Additional flags about how the object should be written
     */
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(image, flags);
        dest.writeString(recognitionResult);
        dest.writeString(suggestion);
        dest.writeByte((byte) (eatable ? 1 : 0));
    }

    /**
     * Describe the kinds of special objects contained in this Parcelable instance
     * @return Always returns 0 as this implementation doesn't include file descriptors
     */
    @Override
    public int describeContents() {
        return 0;
    }

    /**
     * Creator object that generates instances of ImageData from a Parcel
     */
    public static final Creator<ImageData> CREATOR = new Creator<ImageData>() {
        @Override
        public ImageData createFromParcel(Parcel in) {
            return new ImageData(in);
        }

        @Override
        public ImageData[] newArray(int size) {
            return new ImageData[size];
        }
    };

    /**
     * Returns a string representation of the ImageData object
     * @return String containing all field values
     */
    @Override
    public String toString() {
        return "ImageData{" +
                "image=" + image +
                ", recognitionResult='" + recognitionResult + '\'' +
                ", ImageURL='" + ImageURL + '\'' +
                ", suggestion='" + suggestion + '\'' +
                ", eatable=" + eatable +
                '}';
    }

    /**
     * Main constructor for creating new ImageData instances
     * @param image The bitmap image
     * @param recognitionResult Text result from image recognition
     * @param suggestion Scientific suggestion about the item
     * @param eatable Whether the item is edible
     */
    public ImageData(Bitmap image, String recognitionResult, String suggestion, boolean eatable) {
        this.image = image;
        this.recognitionResult = recognitionResult;
        this.suggestion = suggestion;
        this.eatable = eatable;
    }

    // Standard getter and setter methods with documentation

    public Bitmap getImage() {
        return image;
    }

    public String getRecognitionResult() {
        return recognitionResult;
    }

    public String getSuggestion() {
        return suggestion;
    }

    public void setImage(Bitmap image) {
        this.image = image;
    }

    public void setRecognitionResult(String recognitionResult) {
        this.recognitionResult = recognitionResult;
    }

    public void setSuggestion(String suggestion) {
        this.suggestion = suggestion;
    }

    public boolean isEatable() {
        return eatable;
    }

    public void setEatable(boolean eatable) {
        this.eatable = eatable;
    }

    public String getImageURL() {
        return ImageURL;
    }

    public void setImageURL(String imageURL) {
        ImageURL = imageURL;
    }

    /**
     * Updates ImageData objects in the list that match the recognition result
     * @param imageDataList List of ImageData objects to update
     * @param response Raw recognition response string
     * @param scientificBasis Scientific explanation to set as suggestion
     * @param eatable Edibility status to set
     */
    public static void updateImageDataByRecognitionResult(ArrayList<ImageData> imageDataList, String response, String scientificBasis, boolean eatable) {
        Iterator<ImageData> imageDataIterator = imageDataList.iterator();
        while (imageDataIterator.hasNext()) {
            ImageData id;
            if ((id = imageDataIterator.next()).getRecognitionResult().equals(response.substring(response.indexOf("食物：") + 3, response.indexOf("{")))) {
                id.setEatable(eatable);
                id.setSuggestion(scientificBasis);
                Log.d("当前ImageData：", id.toString());
            }
        }
    }
}